<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>RSA+AES 加解密</title>
    <script type="text/javascript" src="theme/js/jquery.js"></script>
    <script type="text/javascript" th:src="@{/theme/js/encrypt/aes.js}"></script>
    <script type="text/javascript" th:src="@{/theme/js/encrypt/jsencrypt.min.js}"></script>
    <script type="text/javascript" th:src="@{/theme/js/encrypt/aes.js}"></script>
    <script type="text/javascript" th:src="@{/theme/js/encrypt/pad-zeropadding.js}"></script>
    <script type="text/javascript" th:src="@{/theme/js/encrypt/security.js}"></script>
    <script type="text/javascript" th:src="@{/theme/js/encrypt/random.js}"></script>


    <script type="text/javascript">
        function sendEncrypt(sendData) {
            // 获取16位随机数,当做aes秘钥key
            var randomStr = randomString(16);
            // aes加密
            console.log(sendData);
            var data = Encrypt(sendData, randomStr);
            // rsa加密
            var encrypt = new JSEncrypt();
            encrypt.setPublicKey($("#servicePublicKey").val());
            var encrypted = encrypt.encrypt(randomStr);
            // 创建json对象
            var json = {
                "requestData": data,
                "encrypted": encrypted
            };
            return JSON.stringify(json);

        }
        function receiveEncrypt(result) {
            console.log("返回的数据对象:" + result);
            // rsa解密,获取到aes秘钥
            var decrypt = new JSEncrypt();
            decrypt.setPrivateKey($('#clientPrivateKey').val());
            var aesKey = decrypt.decrypt(result.encrypted);
            // 用aes秘钥进行解密
            var resultData = Decrypt(result.requestData, aesKey);
            console.log("解密的数据：" + resultData);
            return resultData;
        }
        function bt(){


            $.ajax({
                type: "POST",
                url: "encrypt/testEncrypt",
                data: sendEncrypt(JSON.stringify({name:'awef'})),
                dataType: 'json',
                async:false,
                contentType: "application/json",
                success: function (data) {
                    data=  JSON.parse(receiveEncrypt(data));
                    console.log(data["result"]);
                }
            });
        }

    </script>
</head>
<body>
<h2>服务端公钥：</h2>
<textarea id="servicePublicKey" rows="5" cols="45">MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcerPyjFWlg4U2ItfvWx7f06c3eySbBltl6ohm6sdn6SGsrgF1Xrw7/+ktRWzdzVDmE6epMIvPjbvqDHJThrNGS6y3MJrVV5rEKWYoomo2FIgNMb0SC+wF/LFtNyc9eFEUIpyYRYDsgfmViMKQrO4NTvfgjiwLi7aVTEhpTjHQ9wIDAQAB</textarea>
<h2>客户端私钥：</h2>
<textarea id="clientPrivateKey" rows="5" cols="45">MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAM1cUiuKGVggCe1GetDORZdN/Cv2zKr/QiKJ1sLO6Obh6rghyfvWhGST78NC4/0AGxvg+SssUZmKmlajUDCQa+fCVDBiwEhqKO/fkKB2bWRwtW6iX1BFuhnnI+fKHmdo0WCYp4RkyHrW4PJ5UouLb3ypNC3JMmtJoFlworYmIpOhAgMBAAECgYEAkoQ5xAKljnBdZMKSQYPJtufW8O8Gm0G/R7EcXqUNGh05P+Irn9gvYSsDN0b0Lr/yl3NtktXt6iQvdXzTa7K+qLXC2R+8FEVghxuzsSesG+aE6jDmPPCRaL7N7HLYTNsWz/gj0y5D91mZOo5DxF53OqjG/Un3DI3BiiQT/9J/9PkCQQD2TLnuIDLmk2T1Ez0joFoCjmxwdqYTVi0oMWigLfWRdEunqRxZ/S6GKdEXXQ+QQvIdZNGxtg+fl142e6PbZs7rAkEA1XLVY4w1svMQXMJ0tF6UWl9JpQu0qR6HB7ptBsOEu70TG4ZSTlEFhqQTmXDX8B3ffDgYmgeg1Le41BZAt5l8owJALAfjZVKURuwqrDVYRLox3Ssg76oGXoaVZA4RKjGNKQelTr/Itb2xR4LKQmb8lUmihuazJhXqDRm+WfcYf+yNKQJAEj3zMMPN3xJY5qST1spygVq9b4aVlRKP1cBACyDwmh7poCOVqy3PfBdPuYBkZBgZfoNIzvkx7JwbBr00ILzUYwJAS5ednB0nFGjtrIobwDLzNe2m4Whgk+/e3iiUJYILi83xGlNfWHpvVaYq2xdhbt6T2cD6sv68t9mN7+XaVz0bMQ==</textarea>
<input type="button" onclick="bt()" value="提交"/>
</body>
</html>